package ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import controller.ATMApp;

import db.Account;
import db.BankDatabase;

public class WithdrawCashMenu extends JPanel {
	private JLabel lblXA9;
	private JLabel lblWithdrawCashMenu;
	private JLabel lblPleaseSelectAccount;
	private JLabel lblLogo;
	private JButton btnReturn;
	private JButton btnWithdrawCash;
	private JTable tblAccounts;
	private JScrollPane scpAccounts;
	private DefaultTableModel tableModel;
	private BankDatabase bankDatabase;
	private String accountId = "";

	public WithdrawCashMenu() {

		this.bankDatabase = new BankDatabase();

		setBackground(UIManager.getColor("Label.background"));
		setLayout(null);

		lblXA9 = new JLabel("\u00A92013 CBS HA.IT (Gruppe XA-9)\t\t ");
		lblXA9.setForeground(UIManager.getColor("Button.darkShadow"));
		lblXA9.setBackground(UIManager.getColor("Button.darkShadow"));
		lblXA9.setHorizontalAlignment(SwingConstants.CENTER);
		lblXA9.setFont(new Font("Eurostile", Font.PLAIN, 11));
		lblXA9.setBounds(0, 423, 735, 50);
		add(lblXA9);

		btnReturn = new JButton("Return to main menu");
		btnReturn.setForeground(UIManager.getColor("CheckBox.select"));
		btnReturn.setFont(new Font("Eurostile", Font.PLAIN | Font.BOLD, 13));
		btnReturn.setBounds(159, 323, 200, 50);
		add(btnReturn);

		lblWithdrawCashMenu = new JLabel("Withdraw cash menu");
		lblWithdrawCashMenu.setOpaque(true);
		lblWithdrawCashMenu.setBackground(UIManager
				.getColor("Label.background"));
		lblWithdrawCashMenu.setHorizontalAlignment(SwingConstants.CENTER);
		lblWithdrawCashMenu.setForeground(Color.BLACK);
		lblWithdrawCashMenu.setFont(new Font("Eurostile", Font.BOLD, 24));
		lblWithdrawCashMenu.setBounds(0, 94, 735, 28);
		add(lblWithdrawCashMenu);

		lblPleaseSelectAccount = new JLabel(
				"Please select which account you want to insert money");
		lblPleaseSelectAccount.setHorizontalAlignment(SwingConstants.CENTER);
		lblPleaseSelectAccount.setForeground(Color.BLACK);
		lblPleaseSelectAccount.setFont(new Font("Eurostile", Font.PLAIN, 14));
		lblPleaseSelectAccount.setBounds(0, 121, 735, 16);
		add(lblPleaseSelectAccount);

		btnWithdrawCash = new JButton("Withdraw cash");
		btnWithdrawCash.setFont(new Font("Eurostile", Font.BOLD, 13));
		btnWithdrawCash.setBounds(369, 321, 200, 55);
		add(btnWithdrawCash);

		lblLogo = new JLabel("");
		lblLogo.setIcon(new ImageIcon(
				"/Users/martinotto-sidelmann/Desktop/Zombium.png"));
		lblLogo.setBounds(530, 12, 200, 70);
		add(lblLogo);

		scpAccounts = new JScrollPane();
		scpAccounts.setBounds(159, 149, 415, 160);
		scpAccounts.setFont(new Font("Eurostile", Font.PLAIN, 13));
		add(scpAccounts);

		this.tableModel = new DefaultTableModel();

		tblAccounts = new JTable(this.tableModel);
		tblAccounts.setRowSelectionAllowed(true);
		 ListSelectionModel cellSelectionModel = tblAccounts.getSelectionModel();
		    cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		    cellSelectionModel.addListSelectionListener(new ListSelectionListener() {
		    	public void valueChanged(ListSelectionEvent e) {
		    	  if(e.getValueIsAdjusting()){
		    		  return;
		    	  }

		        int[] selectedRow = tblAccounts.getSelectedRows();
		        int[] selectedColumns = tblAccounts.getSelectedColumns();
		        
		        if(selectedRow.length <= 0) {
		        	return;
		        }
		        
		        String selectedData = tblAccounts.getValueAt(selectedRow[0], 0).toString();
		        accountId = selectedData;
		        System.out.println("Selected: " + selectedData);
		      }

		    });

		this.tableModel.addColumn("AccountId");
		this.tableModel.addColumn("Accountnumber");
		this.tableModel.addColumn("Balance");

		tblAccounts.setEnabled(true);
		scpAccounts.setViewportView(tblAccounts);
		tblAccounts.setBackground(Color.WHITE);
		tblAccounts.setRowHeight(25);
		tblAccounts.setFont(new Font("Eurostile", Font.PLAIN, 11));
		tblAccounts.getTableHeader();
		
	}

	public JButton getBtnWithdrawCash() {
		return btnWithdrawCash;
	}

	public JButton getBtnReturn() {
		return btnReturn;
	}

	public String getAccountId() {
		return this.accountId;
	}
	
	
	public void displayAccounts(int loginId) {
		

		this.tableModel.setRowCount(0); // flush the table
		this.tblAccounts.repaint();
		
		ArrayList<Account> accounts = this.bankDatabase.getAccounts(loginId);

		for (Account account : accounts) {
			System.out.println(account.getAccountId());
			Object[] data = { account.getAccountId(), account.getAccountName(),
					account.getAmount() };
			this.tableModel.addRow(data);
		}
	}

	public void addActionListener(ActionListener l) {
		btnReturn.addActionListener(l);
		btnWithdrawCash.addActionListener(l);
		
	}

	
}
	
